﻿using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Navigation;
using com.IronOne.BoardPACWinApp.Sync;
using com.IronOne.BoardPACWinAppService.Auth;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace com.IronOne.BoardPACWinApp.View.Page
{
    /// <summary>
    ///     An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class WipedOutPage
    {
        private IAuthService _authService;

        public WipedOutPage()
        {
            InitializeComponent();
            _authService = new AuthService();
        }

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            try
            {
                base.OnNavigatedTo(e);

                var msg = e.Parameter as string;
                if (String.IsNullOrEmpty(msg))
                    ErrorMsgTextBlock.Text =
                        "this device is not authorized to use this application.\nplease contact the system administrator";
                else
                {
                    msg = msg.Replace(". ", "\n");
                    ErrorMsgTextBlock.Text = msg.ToLower();
                }

                SyncManager.StopSync();
                await _authService.WipeoutDevice();
            }
            catch (Exception)
            {
            }
        }

        private void LayoutAwarePage_Unloaded(object sender, RoutedEventArgs e)
        {
            _authService = null;
        }
    }
}